//
//  NSArray+Category.m
//  iOS-Category
//
//  Created by 庄BB的MacBook on 16/7/20.
//  Copyright © 2016年 BBFC. All rights reserved.
//

#import "NSArray+Category.h"

@implementation NSArray (Category)

- (BOOL)isEmpty {
    return (self == nil) || (self.count <= 0);
}

- (NSArray *)reversedArray {
    return [NSArray reversedArray:self];
}

+ (NSArray *)reversedArray:(NSArray *)array {
    NSMutableArray *arrayTemp = [NSMutableArray arrayWithCapacity:[array count]];
    NSEnumerator *enumerator = [array reverseObjectEnumerator];
    
    for (id element in enumerator) {
        [arrayTemp addObject:element];
    }
    
    return array;
}

- (BOOL)isSameElements
{
    if(![self isEmpty])
    {
        id index0 = [self safeObjectAtIndex:0];
        int count = 0;
        
        for (id element in self) {
            if([element isEqual:index0])
            {
                count++;
            }
        }
        
        if (count < self.count) {
            return NO;
        }
        else
            return YES;
    }
    
    return NO;
}


- (NSString *)transToJSONString
{
    NSData *paramsJSONData = [NSJSONSerialization dataWithJSONObject:self options:0 error:nil];
    return [[NSString alloc] initWithData:paramsJSONData encoding:NSUTF8StringEncoding];
}

/** 追加数据 */
-(NSArray *)appendArray:(NSArray *)newArray{
    
    //异常处理
    if(newArray == nil || newArray.count ==0) return self;
    
    NSMutableArray *arrM = [NSMutableArray arrayWithArray:self];
    
    [arrM addObjectsFromArray:newArray];
    
    return arrM;
}


/** indexPathys计算 */
+(NSArray *)indexPathsWithStartIndex:(NSUInteger)startIndex count:(NSUInteger)count{
    
    if(count == 0) return nil;
    
    NSMutableArray *indexPathsM = [NSMutableArray arrayWithCapacity:count];
    
    for (NSUInteger i = startIndex; i<startIndex + count; i++) {
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        
        [indexPathsM addObject:indexPath];
    }
    
    return indexPathsM;
}

@end
